package net.onceface.web.listener;

import javax.servlet.ServletContextEvent;

import net.onceface.infra.spring.ioc.InstanceFactory;
import net.onceface.infra.spring.ioc.SpringInstanceProvider;

import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

/**
 * 一个Web监听器，扩展并取代Spring的ContextLoaderListener，将SpringIoC整合到InstanceFactory中。
 */
public class OncefaceContextLoaderListene extends ContextLoaderListener {

        @Override
	public void contextInitialized(ServletContextEvent event) {
		super.contextInitialized(event);
		WebApplicationContext applicationContext = WebApplicationContextUtils.getRequiredWebApplicationContext(event.getServletContext());
		SpringInstanceProvider springProvider = new SpringInstanceProvider(applicationContext);
		InstanceFactory.setInstanceProvider(springProvider);
	}

}
